home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 001 / intrcom.cq / intrcom.c
Text File  |  1985-05-09  |  7KB  |  288 lines

  1. /*
  2.     Routine to provide interrupt driven I/O
  3.     ---------------------------------------
  4.         Bill Hohensee
  5.         Microcomputer Systems Lab
  6.         1408 West University Avenue
  7.         Urbana, Illinois  61801
  8.         [217] 333-5272
  9.  
  10.     This routine is the basic bare minimum of a interrupt driven
  11.     I/O scheme for the IBM-PC/XT.  It assumes communications thru
  12.     COM1.  As characters come across the RS232 of COM1 they cause
  13.     and interrupt which places the characters into a buffer.
  14.  
  15.     The main routine simply sits, and monitors any change in the
  16.     in either the communications buffer, or from the keyboard. As
  17.     characters are typed, they are sent up the line.  As they are
  18.     received, they are displayed.  (Note things would be much faster
  19.     if characters would put to the screen via the "write" function
  20.     as opposed to the putchar.)  
  21.  
  22.     This code benefits greatly from the help of Computer Innovations
  23.     staff, and Mr. Bob Kuphal at Bell Labs, Napier, Illinois.
  24. */
  25.  
  26. #include "stdio.h"
  27.  
  28. #define CTRL_C 3
  29. #define COM1 0
  30. #define COM2 1
  31. #define BAUD_300 2
  32. #define BAUD_600 3
  33. #define BAUD_1200 4
  34. #define BAUD_2400 5
  35. #define BAUD_4800 6
  36. #define BAUD_9600 7
  37. #define ODD 1
  38. #define NONE 2
  39. #define EVEN 3
  40. #define STOP_1 0
  41. #define STOP_2 1
  42. #define BITS_7 2
  43. #define BITS_8 3
  44.  
  45. #define COMINT_VEC 12            /* Comm vector interrupt number */
  46. #define COMBUF_SIZE 10240        /* Communications buffer size */
  47. #define LCR    0x3fb            /* Line Control Register */
  48. #define MCR    0x3fc            /* Modem Control Register */
  49. #define MSR    0x3fe            /* Modem Status Register */
  50. #define LSR    0x3fd            /* Line Status Register */
  51. #ifdef skip
  52. #define IOR    0x3f8            /* Communications I/O Register */
  53. #define IER    0x3f9            /* Interrupt Enable Register */
  54. #define RX_BUFFER 0x3f8            /* RX buffer (register) */
  55. #endif
  56. #define INTA00    0x20            /* 8259 Comm board COM1 Register 0 */
  57. #define INTA01    0x21            /* 8259 Comm board COM1 Register 1 */
  58. #define EOI 0x20            /* 8259 End of Interrupt */
  59.  
  60. struct regval { unsigned int ax,bx,cx,dx,si,di,ds,es; } ;
  61.  
  62.         /* External Declaractions */
  63.  
  64. char combuf[COMBUF_SIZE];    /* Communications buffer filled by comint */
  65. int cptr,            /* Index into commuications buffer */
  66.     head;            /* Current head of commuications buffer */
  67. FILE *outp,*inp;    /* File ptrs for saving/sending files   */
  68.  
  69. int IOR,IER,RX_BUFFER;    /*  ints for defines as test */
  70. int val_array[] = { 0x3f8,0x3f9,0x3f8 }; 
  71. /* End of test */
  72.  
  73.     /***************************************************
  74.     *        Allow Communications Interrupt            *
  75.     ***************************************************/
  76.  
  77. setcom()
  78. {
  79.     unsigned char bite, inportb();
  80.  
  81.     outportb(MCR, 0x0B);            /* Set MCR to allow ints */
  82.     inportb (MSR, bite);            /* Clear Modem Status Reg */
  83.     inportb (LSR, bite);            /* Clear Line Status Reg */
  84.     inportb (val_array[0], bite);            /* Clear I/O Register */
  85.     bite = inportb(LCR);
  86.     bite &= 0x7f;
  87.     outportb(LCR, bite);            /* Set DLAB to 0 */
  88.     outportb(val_array[1], 0x01);            /* Enable Receive Data Ints */
  89.     bite = inportb(INTA01);
  90.     bite &= 0xef;
  91.     outportb(INTA01, bite);            /* allow COM1 ints */
  92. }
  93.  
  94.  
  95.     /***************************************************
  96.     *     Put character to the communications port     *
  97.     ***************************************************/
  98.  
  99. putrs(c)
  100. char c;
  101. {
  102.     int flag;
  103.     unsigned char inportb();
  104.  
  105.  
  106.     outportb(MCR, 0x0b);    /* Request to send, and data terminal ready */
  107. wait1:
  108.     flag = inportb(LSR);
  109.     if ((flag & 0x20) == 0) goto wait1;
  110.     outportb(val_array[0], c);
  111.     return(0);
  112. }
  113.  
  114.  
  115.     /***************************************************
  116.     *  Get character from communications buffer ...    *
  117.     *  Return it if available ... else return 0        *
  118.     ***************************************************/
  119.  
  120. getrs()
  121. {
  122.     int k;
  123.     k = cptr;
  124.     if (k == head)            /* No character in buffer */
  125.         return(0);
  126.     if (k > head)             /* If tail > head, get character */
  127.         return(combuf[head++]);
  128.     if (head < COMBUF_SIZE)
  129.         return(combuf[head++]);
  130.     head = 0;            /* Reset head pointer */
  131.     if (head < k)
  132.         return(combuf[head++]);
  133.     printf("ERROR is getrs()\n");
  134. }
  135.  
  136.  
  137.     /***************************************************
  138.     *        Communications Interrupt Routine          *
  139.     ***************************************************/
  140.  
  141. comint()
  142. {
  143.     extern char combuf[];
  144.     extern int cptr;
  145.     unsigned char inportb(), cbuf;
  146.     unsigned int  lsr_data;
  147.  
  148.     lsr_data = inportb(LSR);
  149.     if ((lsr_data & 0x01) == 0) {        /* Check for char intrpt */
  150.         outportb(MCR, 0x0b);        /* Reset MCR */
  151.         outportb(INTA00, EOI);        /* Send EOI to 8259 */
  152.         return;
  153.     }
  154.     cbuf = inportb(val_array[2]);    /* Read RBR for character */
  155.     if (cptr >= COMBUF_SIZE)    /* Reset ring buffer pointer */
  156.         cptr = 0;
  157.     combuf[cptr++] = cbuf;        /* Add it to buffer */
  158.     outportb(MCR, 0x0b);        /* Reset MCR */
  159.     outportb(INTA00, EOI);        /* Send EOI to 8259 */
  160.     return;
  161. }
  162.  
  163.  
  164.  
  165.     /******************************************************
  166.     *  Alternate between reading keyboard and com buffer  *
  167.     ******************************************************/
  168.  
  169. main()
  170. {
  171.    extern char combuf[];
  172.     extern int  cptr;
  173.     int index;
  174.     int x;
  175.     char rs, c;
  176.     com_rst(COM1,BAUD_1200,NONE,STOP_1,BITS_8);
  177.     setcom();    
  178.     intrinit(comint, 5000, COMINT_VEC);    
  179.     index = cptr = 0;        
  180.     outp=fileget("out");
  181.     for (;;) {
  182.         if ((rs = getrs()) != 0) {    /* get char from com buffer */
  183.          putc(rs,outp);
  184.          putchar(rs);
  185.         }
  186.        if (key_scan() != EOF) {    /* get char from keyboard */
  187.             c = key_getc() & 0x00ff;
  188.             if (c == CTRL_C)
  189.                 break;
  190.             else {
  191.                 putrs(c);    /* send char over com port */
  192.             }
  193.         }
  194.     }
  195.     intrrest(COMINT_VEC);
  196. }
  197.  
  198. /* PROGRAM TO OPEN AND/OR CREATE A FILE */
  199. fileget(type)
  200. char *type;
  201.  
  202. {
  203. int retcode,c,i;
  204. int inlen,outlen;
  205. char inpf[15],outf[40],buff[80];
  206.      printf("\n%s is file type",type);
  207.  
  208.     if( *type == 'o' || *type == 'O') goto outfile;
  209.     if (*type != 'i' || *type !='I') { printf ("\n bad file type");return (-1);}
  210. /* Get input file name and check its existence  */
  211.  
  212. do {
  213. printf("\n\n\n\tNAME OF TRANSMIT FILE?  : ");
  214.  
  215. retcode=fgets(inpf,15,stdin);
  216. if (retcode==0) printf("\n\tINCORRECT READ OF FILE NAME");
  217.         inpf[strlen(inpf)-1]='\0';
  218.  
  219.  
  220.     if (!fexist(inpf))  
  221.            {
  222.                  printf("Cant open input file");
  223.              printf("\nDo you want to try to spell it right?");
  224.              printf("\nYES OR NO ?");   
  225.                   if(toupper(fgets(buff,80,stdin)=='N'))    {
  226.                    printf("Let's go home");exit();      }
  227.               }
  228.     else break;
  229.      }     while(toupper(fgets(buff[0],80,stdin))!='N');
  230.     /* File exists -- open it */
  231.  
  232.       inp=fopen(inpf,"r");
  233.  
  234.   return(inp);
  235.  
  236. outfile:  
  237.   
  238.  
  239. /*  Get output file and check its existence  */
  240.     do {
  241.  
  242. printf("\n\tNAME OF RECEIVE FILE? : ");
  243.         retcode=fgets(outf,15,stdin);
  244.        if(retcode==0) printf("something  wrong"),exit(0);
  245.  
  246.         outf[strlen(outf)-1]='\0';
  247.  
  248.        if (fexist(outf))  {     
  249.       printf("\n\tFILE EXISTS--DO YOU WANT TO OVERWRITE IT?");
  250.         fgets(buff,80,stdin);
  251.    }
  252.        else break;
  253.        }   while(toupper(buff[0])!='Y');
  254.  
  255.         outp=  fopen(outf,"w");
  256.  
  257.       return (outp);
  258. }
  259.  
  260. fexist(filname)
  261.     char *filname;
  262.     {
  263.     char *fp;
  264.     fp=fopen(filname,"r");
  265.     if (fp) 
  266.             {     fclose(fp);
  267.                  return(1);
  268.              }
  269.       else  return(0);
  270.     }
  271.   
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.